package dominion.kingdom;

import java.util.ArrayList;

import dominion.Action;
import dominion.Card;
import dominion.CardTypes;
import dominion.Dominion;
import dominion.Player;
import dominion.Tresure;

public class MitchellsChapel implements Action {

	@Override
	public void execute(Dominion dominion) {
		Player player = dominion.current();
		int silverOrGold = player.count(CardTypes.Silver)
				+ player.count(CardTypes.Gold);
		int times = 0;
		for (Card c : new ArrayList<Card>(player.hand())) {
			int tresure = player.tresure();
			if (3 < times) {
				break;
			}
			if (CardTypes.Estate.equals(c.getType())
					|| CardTypes.Curse.equals(c.getType())) {
				player.trash(c);
				player.hand().remove(c);
				times++;
			}
			if (c instanceof Tresure) {
				Tresure t = (Tresure) c;
				int tv = t.value();
				if (tresure < 6) {
					if (1 < silverOrGold || tresure == 4 || tresure == 5) {
						if (tv == 1) {
							player.trash(c);
							player.hand().remove(c);
							times++;
						}
					}
				} else if (tresure == 7 && tv == 1) {
					player.trash(c);
					player.hand().remove(c);
					times++;
					break;
				} else if (7 < (tresure - tv)) {
					player.trash(c);
					player.hand().remove(c);
					times++;
				}
			}
		}
	}

	@Override
	public CardTypes getType() {
		return CardTypes.Chapel;
	}

	@Override
	public String toString() {
		return getType().name();
	}
}
